<template>
    <div class="main-content">
      <cardBox title="目录" icon="&#xe6f2;" class="left-panel">
          <treeMenu :treeData="menuList" name="indeX_NAME" ref="treeMenu" v-if="menuList.length > 0" isRightMenu
          @addMenuItem="handleAddMenu"
          @delMenuItem="handleDelMenu"
          @editMenuItem="handleAddMenu"
          @menuLeftClick="handleMenuClick"/>
      </cardBox>

      <div class="right-panel">
        <el-card>
            <div slot="header" class="card-title">
                <div>指标关联测点<span v-if="this.getINDEXCLASSInfo">({{ this.getINDEXCLASSInfo }})</span></div>
            </div>
            <el-table :data="tableList1" stripe>  
                <el-table-column prop="point" label="POINT"  align="center"></el-table-column>
                <el-table-column prop="poinT_NAME" label="POINT名称" align="center"></el-table-column>
                <el-table-column prop="comp" label="关联测点" align="center">
                  <template slot-scope="scope">
                    <span style="margin-right: 6px;">{{ scope.row.linK_POINT_NAME }}</span>
                    <el-button type="primary" size="mini" icon="el-icon-s-tools" @click="handleEditList1(scope.row)" circle style="padding:5px;"></el-button>
                  </template>
                </el-table-column>
                <el-table-column prop="compname" label="使用分量" align="center"></el-table-column>
                <el-table-column prop="tp" label="多点关联" align="center">
                  <template slot-scope="scope">
                    <p v-if="scope.row.tp == 1">是</p>
                    <p v-else>否</p>
                  </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" align="center" width="150">
                    <template slot-scope="scope">
                        <el-link type="danger" size="mini" icon="el-icon-delete" @click="handleDelList(1,scope.row)" :underline="false" >删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>
        <el-card>
            <div slot="header" class="card-title">
                <div>指标关联指标</div>
            </div>
            <el-table :data="tableList5" stripe>  
                <el-table-column prop="index" label="INDEX"  align="center"></el-table-column>
                <el-table-column prop="linK_INDEX_NAME" label="INDEX名称" align="center"></el-table-column>
                <el-table-column prop="comp" label="关联指标" align="center">
                  <template slot-scope="scope">
                    <span style="margin-right: 6px;">{{ scope.row.indeX_NAME }}</span>
                    <el-button type="primary" size="mini" icon="el-icon-s-tools" @click="handleEditList5(scope.row)" circle style="padding:5px;"></el-button>
                  </template>
                </el-table-column>
                <el-table-column prop="comP_NAME" label="使用分量" align="center"></el-table-column>
                <el-table-column prop="tp" label="多点关联" align="center">
                  <template slot-scope="scope">
                    <p v-if="scope.row.tp == 1">是</p>
                    <p v-else>否</p>
                  </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" al ign="center" show-overflow-tooltip></el-table-column>
                <el-table-column label="操作" align="center" width="150">
                    <template slot-scope="scope">
                        <el-link type="danger" size="mini" icon="el-icon-delete" @click="handleDelList(5,scope.row)" :underline="false" >删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-card>
            <div slot="header" class="card-title">
                <div>指标参数</div>
            </div>
            <el-table :data="tableList2" stripe>  
                <el-table-column prop="param" label="参数"  align="center"></el-table-column>
                <el-table-column prop="paraM_Name" label="参数名" align="center"></el-table-column>
                <el-table-column prop="value" label="取值" align="center">
                  <template slot-scope="scope">
                    <el-input placeholder="请输入参数值" v-model="scope.row.value" clearable
                    size="small" 
                    @blur="handleSetListValue(1,scope.row,$event)" @clear="handleDelList(2,scope.row)"
                    ></el-input>
                  </template>
                </el-table-column>
                <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip></el-table-column>
                <el-table-column prop="unit" label="单位" align="center"></el-table-column>
            </el-table>
        </el-card>

        <el-card>
            <div slot="header" class="card-title">
                <div>指标控制值</div>
            </div>
            <el-table :data="tableList3" stripe>  
                <el-table-column prop="control" label="控制值"  align="center"></el-table-column>
                <el-table-column prop="controL_NAME" label="控制值名称" align="center"></el-table-column> 
                <el-table-column prop="value" label="取值" align="center">
                  <template slot-scope="scope">
                    <el-input placeholder="请输入参数值" v-model="scope.row.value" clearable
                    size="small" @blur="handleSetListValue(2,scope.row,$event)" @clear="handleDelList(3,scope.row)"></el-input>
                  </template>
                </el-table-column> 
                <el-table-column prop="remark" label="备注" align="center" show-overflow-tooltip></el-table-column>
                <el-table-column prop="unit" label="单位" align="center"></el-table-column> 
            </el-table>
        </el-card>

        <el-card>
            <div slot="header" class="card-title">
                <div>过程线与变位图关联</div>
                <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAddList(4)">新增</el-button>
            </div>
            <el-table :data="tableList4" stripe>  
                <el-table-column prop="catalog" label="类型"  align="center">
                  <template slot-scope="scope">
                    <p v-if="scope.row.catalog == 0">过程线</p>
                    <p v-if="scope.row.catalog == 1">变位图</p>
                  </template>
                </el-table-column>
                <el-table-column prop="变位图" label="测点或变位图"  align="center">
                  <template slot-scope="scope">
                    <p v-if="scope.row.catalog == 0">{{ scope.row.poinT_NAME }}　{{ scope.row.comp }}</p>
                    <p v-if="scope.row.catalog == 1">{{ scope.row.bwtName  }}</p>
                  </template>
                </el-table-column>
                <el-table-column label="操作" align="center" width="150">
                    <template slot-scope="scope">
                        <el-link type="primary" size="mini" icon="el-icon-edit" @click="handleEditList4(scope.row)" :underline="false">修改</el-link>
                        <el-link type="danger" size="mini" icon="el-icon-delete" @click="handleDelList4(scope.row.id)" :underline="false" style="margin-left:10px">删除</el-link>
                    </template>
                </el-table-column>
            </el-table>
        </el-card>

        <el-card>
            <div slot="header" class="card-title">
                <div>指标关联巡视</div>
            </div>
            <el-table :data="tableList6" stripe>
              <el-table-column prop="Inspection" label="PATROL"  align="center"></el-table-column>
              <el-table-column prop="Inspection_Name" label="PATROL名称"  align="center"></el-table-column>
              <el-table-column prop="InspectionTreeName" label="关联巡视部位"  align="center">
                <template slot-scope="scope">
                  <span style="margin-right: 6px;">{{ scope.row.InspectionTreeName }}</span>
                  <el-button type="primary" size="mini" icon="el-icon-s-tools" @click="handleEditList(6,scope.row)" circle style="padding:5px;"></el-button>
                </template>
              </el-table-column>
              <el-table-column prop="tp" label="多点关联" align="center">
                  <template slot-scope="scope">
                    <el-tag type="success" size="small" v-if="scope.row.tp == 1">是</el-tag>
                    <el-tag type="danger" size="small" v-else>否</el-tag>
                  </template>
                </el-table-column>
              <el-table-column prop="REMARK" label="备注"  align="center" show-overflow-tooltip></el-table-column> 
            </el-table>
        </el-card>
      </div>

     <!-- 弹窗 -->
       <el-dialog title="目录配置" :visible.sync="dialogMenuVisible" width="1200px" :before-close="handleCancelDialog" v-dialogDrag >
          <div class="dialog-content">
            <el-form :model="menuForm" label-width="110px" :rules="menuRules" ref="menuForm" size="medium">
              <div class="setting-wrapper">
                <!-- 左侧区域 -->
                <div class="setting-left">
                  <div class="form-section">
                    <div class="section-title">基础信息</div>
                    <el-form-item label="目录名称" required prop="indeX_NAME" class="form-item-enhanced">
                      <el-input 
                        v-model="menuForm.indeX_NAME" 
                        placeholder="请输入目录名称"
                        prefix-icon="el-icon-tickets"
                        clearable
                      ></el-input>
                    </el-form-item>
                    
                    <el-form-item label="指标类型" prop="indeX_CLASS_ID" required class="form-item-enhanced">
                      <div class="tree-container-dialog">
                          <treeMenu :treeData="dialogMenuList" name="clasS_NAME" ref="treeClass" id="indeX_CLASS_ID" @menuLeftClick="handleDialogMenuClick"/>
                      </div>
                    </el-form-item>
                  </div>
                </div>
                
                <!-- 右侧区域 -->
                <div class="setting-right">
                  <div class="form-section">
                    <div class="section-title">数值设置</div>
                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="最小值" class="form-item-enhanced"> 
                          <el-input-number 
                            v-model="menuForm.minn" 
                            controls-position="right"
                            :min="0"
                            class="full-width-input"
                          ></el-input-number>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="最大值" class="form-item-enhanced"> 
                          <el-input-number 
                            v-model="menuForm.maxn" 
                            controls-position="right"
                            :min="menuForm.minn || 0"
                            class="full-width-input"
                          ></el-input-number>
                        </el-form-item>
                      </el-col>
                    </el-row>
                    
                    <el-row :gutter="20">
                      <el-col :span="12">
                        <el-form-item label="权重" class="form-item-enhanced"> 
                          <el-input-number 
                            v-model="menuForm.weightvalue" 
                            :min="0" 
                            :max="1" 
                            :step="0.1"
                            controls-position="right"
                            class="full-width-input"
                          ></el-input-number>
                          <div class="input-tip">取值范围: 0 ~ 1</div>
                        </el-form-item>
                      </el-col>
                      <el-col :span="12">
                        <el-form-item label="是否最底层" class="form-item-enhanced switch-item"> 
                          <el-switch 
                            v-model="menuForm.iS_BOTTOM" 
                            active-color="#13ce66" 
                            inactive-color="#ff4949"  
                            :active-value="1" 
                            :inactive-value="0"
                            active-text="是最底层"
                            inactive-text="非最底层"
                          ></el-switch>
                          <div class="switch-tip">最底层菜单不可添加子项</div>
                        </el-form-item>
                      </el-col>
                    </el-row>
                  </div>
                  
                  <!-- 说明区域 -->
                  <div class="form-section" v-if="treeDataEdit == 1">
                    <div class="section-title">详细说明</div>
                    <el-form-item label="说明" class="form-item-enhanced"> 
                      <el-input
                        type="textarea"
                        :rows="4"
                        placeholder="请输入内容说明"
                        v-model="menuForm.description"
                        resize="none"
                        show-word-limit
                        maxlength="500"
                        class="description-textarea"
                      ></el-input>
                      <el-button type="primary" size="small" @click="tagSettingDialog = true" icon="el-icon-price-tag">添加标签</el-button>
                    </el-form-item>
                  </div>
                  
                  <!-- 评判设置 -->
                  <div class="form-section" v-if="menuForm.iS_BOTTOM == 0">
                    <div class="section-title">评判设置</div>
                    <el-form-item label="评判方式" class="form-item-enhanced"> 
                      <el-radio-group v-model="menuForm.judgetype">
                        <el-radio :label="0" border>按最大值评判</el-radio>
                        <el-radio :label="1" border>按权重评判</el-radio>
                        <el-radio :label="2" border>公式判断</el-radio>
                      </el-radio-group>
                    </el-form-item>
                    
                    <el-form-item label="权重类型" v-if="menuForm.judgetype == 1" class="form-item-enhanced"> 
                      <el-radio-group v-model="menuForm.weighttype">
                        <el-radio :label="0">平均权重</el-radio>
                        <el-radio :label="1">指定权重</el-radio>
                      </el-radio-group>
                    </el-form-item>
                  </div>
                  
                  <!-- 公式设置 -->
                  <div class="form-section" v-if="menuForm.judgetype == 2">
                    <div class="section-title">公式设置</div>
                    <el-form-item label="计算公式" class="form-item-enhanced"> 
                      <el-input
                        type="textarea"
                        :rows="4"
                        placeholder="请输入计算公式"
                        v-model="menuForm.formula"
                        resize="none"
                        class="formula-textarea"
                      ></el-input>
                      <el-button type="primary" size="small" @click="tagSettingDialog = true" icon="el-icon-price-tag">添加标签</el-button>
                    </el-form-item>
                  </div>
                </div>
              </div>
            </el-form>
          </div>
          
          <div class="dialog-footer">
            <el-button @click="handleCancelDialog" size="small" class="cancel-btn" icon="el-icon-close" round>取 消</el-button>
            <el-button type="primary" @click="handleAddMenuSave" size="small" class="confirm-btn" icon="el-icon-check" round>保存配置</el-button>
          </div>
        </el-dialog>

        <tagSet :stationId="stationId" :visible="tagSettingDialog" :tagSettingData="tagSettingData" isIndicator isNoTime isComprehensive
        @handleSaveEditTag="handleSaveEditTag" @handleCloseDialog="handleCloseDialog"/>

      
    </div>
</template>

<script>
import bus from '@/utils/eventBus.js';
import cardBox from "../../components/base/cardBox.vue";
import treeMenu from '@/components/base/treeMenu.vue'
import {scuRmIndexList,GetINDEXList,GetPOINList,indexParamList,indexControlList,indexLinkList,GetInspectionLists,indexPointDel,classParamEdit,classParamAdd,indexLinkIndexDel,classControlEdit,classControlAdd,indexParamSet,compLinkEdit,compLinkAdd,indexParamDel,classLinkIndexEdit,classLinkIndexAdd,indexControlSet,InspectionEdit,InspectionAdd,indexControlDel,scuRmIndexClassCompEdit,scuRmIndexClassCompAdd,getINDEX_CLASS,scuRmIndexClassList,scuRmIndexAdd,scuRmIndexEdit,scuRmIndexDel  } from '@/api/https'
import tagSet from '@/components/base/tagSet.vue'
import calculationCorrelation from '@/components/page/setting/CalculationCorrelation.vue'
export default {
  name: 'IndicatorType',
  components:{calculationCorrelation,cardBox,treeMenu,tagSet},
  data() {
    return {
      stationId: sessionStorage.getItem("stationId"),
      menuList:[],
      treeID:'',
      dialogMenuVisible:false,
      menuForm:{},
      treeDataEdit:false,
      currentTreeData:{},
      treeNodeLevel:'',
      tableList1:[],
      tableList2:[],
      tableList3:[],
      tableList4:[],
      tableList5:[],
      tableList6:[],
      listDialogVisible:false,
      listForm:{},
      editList:false,
      DialogNumber:0,
      dialogTitle:'',
      getINDEXCLASSInfo:'',
      menuRules:{
        indeX_NAME: [{required: true, message: '请输入目录名称'}],
        indeX_CLASS_ID:[{required: true, message: '请选择指标类型'}],
        judgetype:[{required: true, message: '请选择评判设置'}],
        weighttype:[{required: true, message: '请选择权重设置'}],
        weightvalue:[{required: true, message: '请输入权重'}],
      },
      dialogMenuVisible:false,
      dialogMenuList:[],
      menuForm:{},
      tagSettingDialog:false,
      tagSettingData:{}
    }
  },
  mounted(){
        bus.$emit('showBasin', false);
        this.onGegMenuList()
        this.getDialogMenuTree()
  },
  activated(){
      bus.$emit('showBasin', false);
  },
  methods: {
    // 目录部分
    onGegMenuList(){
      scuRmIndexList({
        STATION_ID:this.stationId,
        CATALOG:0
      }).then(res=>{
        if(res.isSuccess && res.data && res.data.length > 0){
            this.menuList = res.data
            this.findLeafNodes(this.menuList);
            this.$nextTick(()=>{
                this.$refs.treeMenu.$refs.tree.setCurrentKey(this.treeID);
                this.$emit('getList',this.treeID,this.currentnNodeData)
                this.initData()
            })
        }else{
            this.menuList = []
        }
      })
    },
    findLeafNodes(data) {
        if (data[0].children == null || data[0].children.length === 0){
            this.treeID = data[0].id
            return
        }else{
            this.findLeafNodes(data[0].children);
        }
    },
    handleMenuClick(data){
       this.treeID =  data.indeX_ID
       this.initData()
       getINDEX_CLASS({
          indexClassID:data.indeX_CLASS_ID
        }).then(res=>{
          if(res.isSuccess){
              this.getINDEXCLASSInfo = res.data
          }else{
            this.getINDEXCLASSInfo = ''
          }
        })
    },

    handleAddMenu(data,isEdit,treeNodeLevel) {
        this.currentTreeData = data  || {}
        this.treeDataEdit = isEdit || 0
        this.treeNodeLevel = treeNodeLevel || 1
        if(isEdit){
          this.menuForm = Object.assign({},data)
          this.$nextTick(()=>{
            this.$refs.treeClass.$refs['tree'].setCurrentKey(data.indeX_CLASS_ID)
          })
          
        }else{
          this.menuForm = {
            iS_BOTTOM:0,
            judgetype:0,
            weighttype:0,
            weightvalue:0,
            minn:0,
            maxn:0,
            formula:'',
            description:''
          }
        }
        this.dialogMenuVisible = true
    },
    handleDelMenu(data){
      this.$confirm(`确认删除名称为 ${data.indeX_NAME} 的节点？`,'提示',{
          type:'warning'
      }).then(() => {
          scuRmIndexDel({
              id:data.id
          }).then(res=>{
              if(res.isSuccess){
                this.removeNodeFromTree(data.id)
              }else{
                this.$message.error(res.errorMessage)
              }
          })
      }).catch(() => {}); 
      
    },
    getDialogMenuTree(){
      scuRmIndexClassList().then(res=>{
        if(res.isSuccess){
            this.dialogMenuList = res.data || []
        }else{
            this.dialogMenuList = []
        }
      })
    },
    handleDialogMenuClick(data){
      this.$refs['menuForm'].clearValidate()
      const _data = {...this.menuForm}
      _data.indeX_CLASS_ID = data.indeX_CLASS_ID
      this.menuForm = {..._data}
    },
    handleCancelDialog(){
        this.dialogMenuVisible = false
        this.$refs['menuForm'].clearValidate()
        this.menuForm = {}
    },
    handleAddMenuSave(){
      this.$refs['menuForm'].validate((valid) => {
        if (valid) {
          if(this.treeDataEdit){
            const data = {
              ...this.menuForm,
              statioN_ID:this.stationId,
              upindeX_ID1:this.currentTreeData.upindeX_ID1,
              weighttype:this.menuForm.judgetype == 2 ? 2 : this.menuForm.weighttype || 0,
              weightvalue:this.menuForm.weightvalue || 0,
              description:this.menuForm.description || '',
              formula:this.menuForm.formula || ''
            }
              // 编辑
              scuRmIndexEdit(data).then(res=>{
                  if(res.isSuccess){
                    let node = this.$refs.treeMenu.$refs.tree.getNode(res.data.indeX_ID)
                    if(node){
                      node.data.indeX_NAME = res.data.indeX_NAME
                      node.data.iS_BOTTOM = res.data.iS_BOTTOM
                      node.data.indeX_CLASS_ID = res.data.indeX_CLASS_ID
                      node.data.judgetype = res.data.judgetype || 0
                      node.data.weighttype = res.data.weighttype || 0
                      node.data.weightvalue = res.data.weightvalue || 0
                      node.setData(node.data)
                      
                    }
                    this.$forceUpdate()
                    this.handleCancelDialog()  
                  }else{
                    this.$message.error(res.errorMessage)
                  }
              })
          }else{
          // 新增
          if(this.treeNodeLevel === 1){
              // 新增同级
              const data = {
                ...this.menuForm,
                  id:0,
                  statioN_ID:this.stationId,
                  indeX_ID:0, 
                  upindeX_ID:this.currentTreeData.upindeX_ID || 0,
                  catalog:0, 
                  indeX_ID1:'',
                  upindeX_ID1:this.currentTreeData.upindeX_ID1 ||0,
                  weighttype:this.menuForm.judgetype == 2 ? 2 : this.menuForm.weighttype || 0,
              }
              scuRmIndexAdd(data).then(res=>{
                if(res.isSuccess){
                    if(this.menuList.length == 0){
                        this.onGegMenuList()
                    }else{
                      this.addNodeToTree(res.data,res.data.upindeX_ID)
                    }
                    this.handleCancelDialog()  
                }else{
                  this.$message.error(res.errorMessage)
                }
              })
            }else if(this.treeNodeLevel === 2){
                // 新增子级
                const data = {
                  ...this.menuForm,
                    id:0,
                    statioN_ID:this.stationId,
                    indeX_ID:0,
                    upindeX_ID:this.currentTreeData.indeX_ID || 0,
                    catalog:0,
                    indeX_ID1:this.currentTreeData.indeX_ID1 || '',
                    upindeX_ID1:this.currentTreeData.upindeX_ID1 ||  this.currentTreeData.indeX_ID1,
                    weighttype:this.menuForm.judgetype == 2 ? 2 : this.menuForm.weighttype || 0,
                }
                scuRmIndexAdd(data).then(res=>{
                    if(res.isSuccess){
                      this.addNodeToTree(res.data,res.data.upindeX_ID)
                      this.handleCancelDialog()
                    }else{
                      this.$message.error(res.errorMessage)
                    }
                })
            }
          }
        } else {
          return false;
        }
      }); 
    },
    addNodeToTree(newNode, parentId) {
      const findAndAddNode = (nodes, targetId, newNode) => {
        for (let i = 0; i < nodes.length; i++) {
          if (nodes[i].indeX_ID === targetId) {
            if (!nodes[i].children) {
              this.$set(nodes[i], 'children', []);
            }
            nodes[i].children.push(newNode);
            return true;
          }
          if (nodes[i].children && nodes[i].children.length > 0) {
            const found = findAndAddNode(nodes[i].children, targetId, newNode);
            if (found) return true;
          }
        }
        return false;
      };
      findAndAddNode(this.menuList, parentId, newNode);
      this.$nextTick(() => {
        this.$forceUpdate();
      });
    },

    removeNodeFromTree(nodeId) {
      const removeNode = (nodes, targetId) => {
        for (let i = 0; i < nodes.length; i++) {
          if (nodes[i].indeX_ID === targetId) {
            nodes.splice(i, 1);
            return true;
          }
          if (nodes[i].children && nodes[i].children.length > 0) {
            const removed = removeNode(nodes[i].children, targetId);
            if (removed) return true;
          }
        }
        return false;
      };

      const removed = removeNode(this.menuList, nodeId);
      
      if (removed) {
        this.$nextTick(() => {
          this.menuList = [...this.menuList];
        });
      }
      
      return removed;
    },
    
    handleCloseDialog(){
        this.tagSettingData = {}
        this.tagSettingDialog = false
    },
    handleSaveEditTag(data,codeId){

    },

    // 数据部分
    initData(){
        this.onGetListData1()
        this.onGetListData2()
        this.onGetListData3()
        this.onGetListData4()
        this.onGetListData5()
        this.onGetListData6()    
    },

    onGetListData1(){
        GetPOINList({
            INDEX_ID:this.treeID
        }).then(res=>{
            if(res.isSuccess && res.data.length > 0 ){
                this.tableList1 = res.data || []
            }else{
                this.tableList1 = []
            }
        })
    },

    onGetListData2(){
        indexParamList({
            INDEX_ID:this.treeID
        }).then(res=>{
            if(res.isSuccess && res.data.length > 0 ){
                this.tableList2 =  res.data || []
            }else{
                this.tableList2 = []
            }
        })
    },

    onGetListData3(){
        indexControlList({
            INDEX_ID:this.treeID
        }).then(res=>{
            if(res.isSuccess && res.data.length > 0 ){
                this.tableList3 =  res.data || []
            }else{
                this.tableList3 = []
            }
        })

    },

    onGetListData4(){
        indexLinkList({
            INDEX_ID:this.treeID
        }).then(res=>{
            if(res.isSuccess && res.data.length > 0 ){
                this.tableList4 =  res.data || []
            }else{
                this.tableList4 = []
            }
        })
    },

    onGetListData5(){
        GetINDEXList({
            INDEX_ID:this.treeID
        }).then(res=>{
            if(res.isSuccess && res.data.length > 0 ){
                this.tableList5 =  res.data || []
            }else{
                this.tableList5 = []
            }
        })
    },


    onGetListData6(){
        GetInspectionLists({
            INDEX_ID:this.treeID
        }).then(res=>{
            if(res.isSuccess && res.data.length > 0 ){
                this.tableList6 =  res.data || []
            }else{
                this.tableList6 = []
            }
        })
    },

    handleAddList(num,titleName){
        this.dialogTitle = titleName
        this.DialogNumber = num
        this.listDialogVisible = true
    },

    handleEditList(num,data,titleName){
        this.DialogNumber = num
        this.dialogTitle = titleName
        this.listForm = {...data}
        this.editList = true
        this.listDialogVisible = true
    },
    handleCancelListDialog(){
        this.DialogNumber = 0
        this.listForm = {}
        this.editList = false
        this.listDialogVisible = false
    },
    handleSaveListDialog(dialogData){
        const data = {...dialogData}
        if(this.DialogNumber == 1){
            data.remark = data.remark != null ? data.remark : ''
            data.unit = data.unit != null ?  data.unit : ''
            data.u_EDIT = data.u_EDIT == 1 ? 1 : 0
            if(this.editList){
                classParamEdit(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData1()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }else{
                data.id = 0
                data.indeX_CLASS_ID = this.treeID
                classParamAdd(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData1()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }
        }

        if(this.DialogNumber == 2){
            data.remark = data.remark != null ? data.remark : ''
            data.unit = data.unit != null ? data.unit : ''
            if(this.editList){
                classControlEdit(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData2()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }else{
                data.id = 0
                data.indeX_CLASS_ID = this.treeID
                classControlAdd(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData2()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }
        }

        if(this.DialogNumber == 3){
            if(this.editList){
                compLinkEdit(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData3()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }else{
                data.id = 0
                data.indeX_CLASS_ID = this.treeID
                compLinkAdd(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData3()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }
        }


        if(this.DialogNumber == 4){
            if(this.editList){
                scuRmIndexClassCompEdit(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData4()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }else{
                data.id = 0
                data.indeX_CLASS_ID = this.treeID
                scuRmIndexClassCompAdd(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData4()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }
        }

        if(this.DialogNumber == 5){
            if(this.editList){
                classLinkIndexEdit(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData5()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }else{
                data.id = 0
                data.indeX_CLASS_ID = this.treeID
                classLinkIndexAdd(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData5()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }
        }

        if(this.DialogNumber == 6){
            if(this.editList){
                InspectionEdit(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData6()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }else{
                data.id = 0
                data.indeX_CLASS_ID = this.treeID
                InspectionAdd(data).then(res=>{
                    if(res.isSuccess){
                        this.handleCancelListDialog()
                        this.onGetListData6()
                    }else{
                        this.$message.error(res.errorMessage)
                    }
                })
            }
        }   
    },
    handleDelList(num,data){
        if(num == 1){
            this.$confirm(`确认要删除名称为 ${data.poinT_NAME} 的指标关联测点？`, '提示', {
                type: 'warning'
                }).then(() => {
                    indexPointDel({ID:data.id}).then(res=>{
                        if(res.isSuccess){
                            this.onGetListData1()
                        }else{
                            this.$message.error(res.errorMessage)
                        }
                    })
                }).catch(() => {        
            });
        }

        if(num == 2){
          indexParamDel({ID:data.id}).then(res=>{
            if(res.isSuccess){
              this.onGetListData2()
            }else{
              this.onGetListData2()
              this.$message.error(res.errorMessage)
            }
          }) 
        }

        if(num == 3){
          indexControlDel({ID:data.id}).then(res=>{
              if(res.isSuccess){
                  this.onGetListData3()
              }else{
                this.$message.error(res.errorMessage)
              }
          })
        }

        if(num == 5){
            this.$confirm(`确认要删除名称为 ${data.linK_INDEX_NAME} 的指标关联指标？`, '提示', {
                type: 'warning'
                }).then(() => {
                    indexLinkIndexDel({ID:data.id}).then(res=>{
                        if(res.isSuccess){
                            this.onGetListData5()
                        }else{
                            this.$message.error(res.errorMessage)
                        }
                    })
                }).catch(() => {        
            });
        }
    },
    handleSetListValue(num,data,event){
      if (event && event.relatedTarget && 
        event.relatedTarget.classList.contains('el-icon-circle-close')) {
        return;
      }
      if(num == 1){
        indexParamSet({
          id:data.id,
          indeX_ID:data.indeX_ID,
          param:data.param,
          paraM_Name:data.paraM_Name,
          value:Number(data.value),
          unit:data.unit
        }).then(res=>{
            if(res.isSuccess){
              this.onGetListData2()
            }else{
              this.$message.error(res.errorMessage)
            }
        })
      }

      if(num == 2){
        indexControlSet({
          id:data.id,
          indeX_ID:data.indeX_ID,
          control:data.control,
          controL_NAME:data.controL_NAME,
          value:Number(data.value),
          unit:data.unit,
          remark:data.remark
        }).then(res=>{
            if(res.isSuccess){
              this.onGetListData3()
            }else{
              this.$message.error(res.errorMessage)
            }
        })
      }
      
    },
  }
}
</script>

<style scoped lang="scss">
.main-content {
  display: flex;
  flex-direction: row;
  justify-content: start;
  padding:0px;
    .left-panel {
      width: 350px;
      margin-right: 10px;
      height: calc(100vh - 115px);
    }
    .right-panel{
        flex:1;
        height: calc(100vh - 115px);
        overflow: auto;
        ::v-deep .el-card{
            margin-bottom: 10px;
        }
    }
}
.tree-container {
  overflow: auto;
  height: calc(100vh - 190px)
}
.card-title{
  font-weight: bold;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}


.setting-wrapper {
  display: flex;
  gap: 20px;
}

.setting-left {
  flex: 1;
  min-width: 450px;
}

.setting-right {
  flex: 2;
  min-width: 400px;
}

.form-section {
  margin-bottom: 10px;
  padding: 15px;
  background: #f9fafc;
  border-radius: 4px;
}

.section-title {
  font-weight: bold;
  color: #303133;
  margin-bottom: 15px;
  font-size: 14px;
}

.form-item-enhanced {
  margin-bottom: 18px;
}

.tree-container-dialog{
  .tree-container {
    overflow: auto;
    height: 430px;
  }
}

</style>